package com.jz.community.moduleshoppingguide.home.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.gongwen.marqueen.MarqueeFactory;
import com.gongwen.marqueen.MarqueeView;
import com.jz.community.basecomm.base.BaseX5WebActivity;
import com.jz.community.basecomm.bean.BaseResponseInfo;
import com.jz.community.basecomm.glideConfig.GetBitmapImageListener;
import com.jz.community.basecomm.glideConfig.GlideApp;
import com.jz.community.basecomm.net.rxandroid.OnLoadListener;
import com.jz.community.basecomm.routerUtils.RouterCommonUtils;
import com.jz.community.basecomm.routerUtils.RouterIntentConstant;
import com.jz.community.basecomm.utils.BaseImageLoaderUtils;
import com.jz.community.basecomm.utils.CharacterUtils;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.basecomm.utils.LoggerUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.RoundCornersTransformation;
import com.jz.community.basecomm.utils.RxImageTool;
import com.jz.community.basecomm.utils.RxTextTool;
import com.jz.community.basecomm.utils.RxTimeTool;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.basecomm.utils.WpToast;
import com.jz.community.commview.banner.Banner;
import com.jz.community.commview.banner.BannerConfig;
import com.jz.community.commview.banner.Transformer;
import com.jz.community.commview.banner.listener.OnBannerListener;
import com.jz.community.commview.view.widget.XCRoundRectImageView;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.home.bean.HomeFindBean;
import com.jz.community.moduleshoppingguide.home.model.TimeLimitModel;
import com.jz.community.moduleshoppingguide.home.model.TimeLimitModelImp;
import com.jz.community.moduleshoppingguide.home.ui.activity.ChannelOutActivity;
import com.jz.community.moduleshoppingguide.home.ui.activity.NewbornActivity;
import com.jz.community.moduleshoppingguide.home.ui.activity.ShareWebActivity;
import com.jz.community.moduleshoppingguide.home.utils.HomeBannerGlideImageLoader;
import com.jz.community.moduleshoppingguide.nearshop.ui.activity.NearShopActivity;
import com.jz.community.moduleshoppingguide.nearshop.ui.activity.NearShopGoodsActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RejectedExecutionException;

import cn.iwgang.countdownview.CountdownView;

public class HomeFindAdapter extends BaseMultiItemQuickAdapter<HomeFindBean.EmbeddedBean.ContentBean, BaseViewHolder> {

    private Context context;
    HomeMainRecommendAdapter adapter;
    private TimeLimitModel timeLimitModel;
    //人气推荐
    private final int CHUNNEL_RECOMMENDS_VIEW = 8;
    //新人专区
    private final int CHUNNEL_NEWBORN_VIEW = 9;
    private View homeBannerView;
    private Banner banner;

    public interface GuidePageChangeListener {
        void pageChangeListener(Bitmap bitmap, Palette.Swatch vibrant, View homeBannerView, boolean isBannerData);
    }

    private GuidePageChangeListener guidePageChangeListener;

    public void setGuidePageChangeListener(GuidePageChangeListener guidePageChangeListener) {
        this.guidePageChangeListener = guidePageChangeListener;
    }

    public HomeFindAdapter(List<HomeFindBean.EmbeddedBean.ContentBean> data, Context context) {
        super(data);
        this.context = context;
        timeLimitModel = new TimeLimitModelImp(context);
        addItemType(HomeFindBean.EmbeddedBean.ContentBean.CHUNNEL_BANNERS_VIEW, R.layout.module_shoppingguide_item_home_type_banner);
        addItemType(HomeFindBean.EmbeddedBean.ContentBean.CHUNNEL_ENTRANCES_VIEW, R.layout.module_shoppingguide_item_home_type_rk);
        addItemType(HomeFindBean.EmbeddedBean.ContentBean.CHUNNEL_HEADLINES_VIEW, R.layout.module_shoppingguide_item_home_type_tt);
        addItemType(HomeFindBean.EmbeddedBean.ContentBean.CHUNNEL_365SHOP_VIEW, R.layout.module_shoppingguide_layout_blank);
        addItemType(HomeFindBean.EmbeddedBean.ContentBean.CHUNNEL_SHOP_RECOMMENDS_VIEW, R.layout.module_shoppingguide_item_home_type_hdtj);
        addItemType(HomeFindBean.EmbeddedBean.ContentBean.CHUNNEL_LIMITED_BUYS_VIEW, R.layout.module_shoppingguide_item_home_type_xsg);
        addItemType(HomeFindBean.EmbeddedBean.ContentBean.CHUNNEL_ACTIVITIES_VIEW, R.layout.module_shoppingguide_item_home_type_zt_item);
        addItemType(HomeFindBean.EmbeddedBean.ContentBean.CHUNNEL_BLANK_VIEW, R.layout.module_shoppingguide_layout_blank);
        addItemType(CHUNNEL_RECOMMENDS_VIEW, R.layout.module_shoppingguide_item_home_type_rqtj);
        addItemType(CHUNNEL_NEWBORN_VIEW, R.layout.module_shoppingguide_item_home_type_xrzq);
    }

    @Override
    protected void convert(BaseViewHolder helper, HomeFindBean.EmbeddedBean.ContentBean item) {
        switch (helper.getItemViewType()) {
            case HomeFindBean.EmbeddedBean.ContentBean.CHUNNEL_BANNERS_VIEW:
                setBannerData(helper, item);
                break;
            case HomeFindBean.EmbeddedBean.ContentBean.CHUNNEL_ENTRANCES_VIEW:
                setEntrancesData(helper, item);
                break;
            case HomeFindBean.EmbeddedBean.ContentBean.CHUNNEL_HEADLINES_VIEW:
                setHeadlinesData(helper, item);
                break;
//            case HomeFindBean.EmbeddedBean.ContentBean.CHUNNEL_365SHOP_VIEW:
//                set365Data(helper, item);
//                break;
            case HomeFindBean.EmbeddedBean.ContentBean.CHUNNEL_SHOP_RECOMMENDS_VIEW:
                setShopRecommendsData(helper, item);
                break;
            case CHUNNEL_NEWBORN_VIEW:
                setChunnelNewbornData(helper, item);
                break;
            case HomeFindBean.EmbeddedBean.ContentBean.CHUNNEL_LIMITED_BUYS_VIEW:
                setLimitedBuysData(helper, item);
                break;
            case HomeFindBean.EmbeddedBean.ContentBean.CHUNNEL_ACTIVITIES_VIEW:
                setActivitiesData(helper, item);
                break;
            case CHUNNEL_RECOMMENDS_VIEW:
                setRecommendsData(helper, item);
                break;
        }
    }

    private float SHOW_ADV_IMAGE_RADIO = 750.0f / 306.0f;

    private void setAdvImageRadio(Banner recommendFocusView, View homeBannerView) {
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) recommendFocusView.getLayoutParams();
        float advIvRadio = SHOW_ADV_IMAGE_RADIO;

        int advImageHeight = (int) (SHelper.getScrrenW((Activity) mContext) / advIvRadio);
        layoutParams.height = advImageHeight;
        recommendFocusView.setLayoutParams(layoutParams);

        RelativeLayout.LayoutParams viewParams = (RelativeLayout.LayoutParams) homeBannerView.getLayoutParams();
        viewParams.height = SHelper.dp2px(mContext, 60);
        homeBannerView.setLayoutParams(viewParams);

    }

    /**
     * 轮播状态
     *
     * @param isAutoPlay
     */
    public void setBannerAutoPlay(boolean isAutoPlay) {
        if (isAutoPlay) {
            if (banner != null) {
                banner.isAutoPlay(isAutoPlay);
                banner.invalidate();
            }
        } else {
            if (banner != null){
                banner.isAutoPlay(isAutoPlay);
                banner.invalidate();
            }
        }
    }

    /**
     * 设置Banner数据
     *
     * @param helper
     * @param item
     */
    private void setBannerData(BaseViewHolder helper, HomeFindBean.EmbeddedBean.ContentBean item) {

        banner = helper.getView(R.id.home_main_banner);

        homeBannerView = helper.getView(R.id.home_banner_view_bg);


        banner.setPageMargin();

        //设置banner样式
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        banner.setImageLoader(new HomeBannerGlideImageLoader());
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.Accordion);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(5000);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.CENTER);

        if (item.getChunnelHomePage() != null && item.getChunnelHomePage().size() > 0) {
            List<String> imgUrls = new ArrayList<>();
            for (int i = 0; i < item.getChunnelHomePage().size(); i++) {
                imgUrls.add(item.getChunnelHomePage().get(i).getImage());
            }
            //设置图片集合
            banner.setImages(imgUrls);

            banner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                }

                @Override
                public void onPageSelected(int position) {
                    BaseImageLoaderUtils.getInstance().loadImageBitmap(mContext, item.getChunnelHomePage().get(position).getImage(), new GetBitmapImageListener() {
                        @Override
                        public void success(Bitmap bitmap) {
                            if (!Preconditions.isNullOrEmpty(bitmap) && !Preconditions.isNullOrEmpty(context)) {
                                try {
                                    Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
                                        public void onGenerated(Palette palette) {
                                            // Use generated instance
                                            if (palette != null) {
                                                Palette.Swatch vibrant = palette.getLightVibrantSwatch();
                                                if (vibrant != null) {
                                                    if (guidePageChangeListener != null) {
                                                        guidePageChangeListener.pageChangeListener(bitmap, vibrant, homeBannerView, true);
                                                    }
                                                } else {
                                                    if (guidePageChangeListener != null) {
                                                        guidePageChangeListener.pageChangeListener(null, null, null, false);
                                                    }
                                                }
                                            }
                                        }
                                    });
                                } catch (RejectedExecutionException e) {

                                }
                            }
                        }
                    });
                }

                @Override
                public void onPageScrollStateChanged(int state) {
                }
            });
            banner.setOnBannerListener(position -> {
                if ("2".equals(item.getChunnelHomePage().get(position).getType())) {
                    if ("1".equals(item.getChunnelHomePage().get(position).isShare())) {
                        mContext.startActivity(new Intent(mContext, BaseX5WebActivity.class)
                                .putExtra("url", item.getChunnelHomePage().get(position).getUrl())
                                .putExtra("title", item.getChunnelHomePage().get(position).getName()));
                    } else if ("0".equals(item.getChunnelHomePage().get(position).isShare())) {
                        mContext.startActivity(new Intent(mContext, ShareWebActivity.class)
                                .putExtra("canShare", 1)
                                .putExtra("url", item.getChunnelHomePage().get(position).getUrl())
                                .putExtra("share_url", item.getChunnelHomePage().get(position).getShareLink())
                                .putExtra("share_title", item.getChunnelHomePage().get(position).getShareTitle())
                                .putExtra("share_desc", item.getChunnelHomePage().get(position).getShareDescribe()));
                    }
                } else if ("3".equals(item.getChunnelHomePage().get(position).getType())) {
                    Intent intent = new Intent(mContext, NearShopActivity.class);
                    mContext.startActivity(intent);
                } else if ("4".equals(item.getChunnelHomePage().get(position).getType())) {
                    ARouter.getInstance()
                            .build(RouterIntentConstant.MODULE_SHOPPING_GOODS_DETAIL)
                            .withString("url", item.getChunnelHomePage().get(position).getGoodsLink())
                            .navigation();
                }
            });
            banner.start();
        } else {
            if (guidePageChangeListener != null) {
                guidePageChangeListener.pageChangeListener(null, null, null, false);
            }
            banner.setBackgroundResource(R.mipmap.default_img_2_1);
        }

        setAdvImageRadio(banner, homeBannerView);

    }

    /**
     * 设置入口数据
     *
     * @param helper
     * @param item
     */
    private void setEntrancesData(BaseViewHolder helper, HomeFindBean.EmbeddedBean.ContentBean item) {
        if (item.getChunnelHomePage().size() > 0) {
            setItemShow(helper, true);
        } else {
            setItemShow(helper, false);
        }
        RecyclerView homeTypeRk = helper.getView(R.id.rv_home_type_rk);
        GridLayoutManager linearLayoutManager = new GridLayoutManager(mContext, 5);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        homeTypeRk.setLayoutManager(linearLayoutManager);
        HomeMainMenuAdapter adapter = new HomeMainMenuAdapter(R.layout.module_shoppingguide_item_home_main_menu, item.getChunnelHomePage());
        homeTypeRk.setAdapter(adapter);
    }

    /**
     * 设置头条数据
     *
     * @param helper
     * @param item
     */
    private void setHeadlinesData(BaseViewHolder helper, HomeFindBean.EmbeddedBean.ContentBean item) {
        RelativeLayout home_marquee_adv_layout = helper.getView(R.id.home_marquee_adv_layout);
        if (item.getChunnelHomePage().size() > 0) {
            setItemShow(helper, true);
            SHelper.vis(home_marquee_adv_layout);
        } else {
            setItemShow(helper, false);
            SHelper.gone(home_marquee_adv_layout);
        }
        MarqueeView<RelativeLayout, HomeFindBean.EmbeddedBean.ContentBean.ChunnelHomePageBean> txMarqueeNew = helper.getView(R.id.tx_marquee_new);
        MarqueeFactory<RelativeLayout, HomeFindBean.EmbeddedBean.ContentBean.ChunnelHomePageBean> marqueeFactory = new ComplexViewMF(context);
        marqueeFactory.setData(item.getChunnelHomePage());
        txMarqueeNew.setInAndOutAnim(R.anim.in_top, R.anim.out_bottom);
        txMarqueeNew.setMarqueeFactory(marqueeFactory);
        txMarqueeNew.startFlipping();
    }


    /**
     * 365平台店铺入口
     *
     * @param helper
     * @param item
     */
    private void set365Data(BaseViewHolder helper, HomeFindBean.EmbeddedBean.ContentBean item) {
        if (item.getChunnelHomePage().size() > 0) {
            setItemShow(helper, true);
        } else {
            setItemShow(helper, false);
        }
        ImageView home_main_head_365_1 = helper.getView(R.id.home_main_head_365_1);
        ImageView home_main_head_365_2 = helper.getView(R.id.home_main_head_365_2);
        ImageView home_main_head_365_3 = helper.getView(R.id.home_main_head_365_3);

        ViewGroup.LayoutParams lp_365_1 = home_main_head_365_1.getLayoutParams();
        lp_365_1.width = (SHelper.getScrrenWidth((Activity) mContext) - RxImageTool.dp2px(30)) / 2;
        lp_365_1.height = (SHelper.getScrrenWidth((Activity) mContext) - RxImageTool.dp2px(30)) / 2;
        home_main_head_365_1.setLayoutParams(lp_365_1);

        ViewGroup.LayoutParams lp_365_2 = home_main_head_365_2.getLayoutParams();
        lp_365_2.width = (SHelper.getScrrenWidth((Activity) mContext) - RxImageTool.dp2px(30)) / 2;
        lp_365_2.height = (lp_365_2.width - RxImageTool.dp2px(10)) / 2;
        home_main_head_365_2.setLayoutParams(lp_365_2);

        ViewGroup.LayoutParams lp_365_3 = home_main_head_365_3.getLayoutParams();
        lp_365_3.width = (SHelper.getScrrenWidth((Activity) mContext) - RxImageTool.dp2px(30)) / 2;
        lp_365_3.height = (lp_365_3.width - RxImageTool.dp2px(10)) / 2;
        home_main_head_365_3.setLayoutParams(lp_365_3);


        if (item.getChunnelHomePage().size() > 0) {
            if (item.getChunnelHomePage().size() == 1) {
                GlideApp.with(mContext)
                        .load(item.getChunnelHomePage().get(0).getImage())
                        .placeholder(R.mipmap.default_img_2_1)
                        .error(R.mipmap.default_img_2_1)
                        .into(home_main_head_365_1);

                home_main_head_365_1.setOnClickListener(v -> {
                    Intent intent = new Intent(mContext, ChannelOutActivity.class);
                    intent.putExtra("name", item.getChunnelHomePage().get(0).getName());
                    intent.putExtra("id", item.getChunnelHomePage().get(0).getShowGoodsCategoryId());
                    intent.putExtra("shopId", item.getChunnelHomePage().get(0).getShopId());
                    mContext.startActivity(intent);
                });
            }
            if (item.getChunnelHomePage().size() == 2) {
                GlideApp.with(mContext)
                        .load(item.getChunnelHomePage().get(0).getImage())
                        .placeholder(R.mipmap.default_img_2_1)
                        .error(R.mipmap.default_img_2_1)
                        .into(home_main_head_365_1);
                GlideApp.with(mContext)
                        .load(item.getChunnelHomePage().get(1).getImage())
                        .placeholder(R.mipmap.default_img_2_1)
                        .error(R.mipmap.default_img_2_1)
                        .into(home_main_head_365_2);

                home_main_head_365_1.setOnClickListener(v -> {
                    Intent intent = new Intent(mContext, ChannelOutActivity.class);
                    intent.putExtra("name", item.getChunnelHomePage().get(0).getName());
                    intent.putExtra("id", item.getChunnelHomePage().get(0).getShowGoodsCategoryId());
                    intent.putExtra("shopId", item.getChunnelHomePage().get(0).getShopId());
                    mContext.startActivity(intent);
                });

                home_main_head_365_2.setOnClickListener(v -> {
                    Intent intent = new Intent(mContext, ChannelOutActivity.class);
                    intent.putExtra("name", item.getChunnelHomePage().get(1).getName());
                    intent.putExtra("id", item.getChunnelHomePage().get(1).getShowGoodsCategoryId());
                    intent.putExtra("shopId", item.getChunnelHomePage().get(1).getShopId());
                    mContext.startActivity(intent);
                });
            }
            if (item.getChunnelHomePage().size() == 3) {
                GlideApp.with(mContext)
                        .load(item.getChunnelHomePage().get(0).getImage())
                        .placeholder(R.mipmap.default_img_2_1)
                        .error(R.mipmap.default_img_2_1)
                        .into(home_main_head_365_1);
                GlideApp.with(mContext)
                        .load(item.getChunnelHomePage().get(1).getImage())
                        .placeholder(R.mipmap.default_img_2_1)
                        .error(R.mipmap.default_img_2_1)
                        .into(home_main_head_365_2);
                GlideApp.with(mContext)
                        .load(item.getChunnelHomePage().get(2).getImage())
                        .placeholder(R.mipmap.default_img_2_1)
                        .error(R.mipmap.default_img_2_1)
                        .into(home_main_head_365_3);

                home_main_head_365_1.setOnClickListener(v -> {
                    Intent intent = new Intent(mContext, ChannelOutActivity.class);
                    intent.putExtra("name", item.getChunnelHomePage().get(0).getName());
                    intent.putExtra("id", item.getChunnelHomePage().get(0).getShowGoodsCategoryId());
                    intent.putExtra("shopId", item.getChunnelHomePage().get(0).getShopId());
                    mContext.startActivity(intent);
                });

                home_main_head_365_2.setOnClickListener(v -> {
                    Intent intent = new Intent(mContext, ChannelOutActivity.class);
                    intent.putExtra("name", item.getChunnelHomePage().get(1).getName());
                    intent.putExtra("id", item.getChunnelHomePage().get(1).getShowGoodsCategoryId());
                    intent.putExtra("shopId", item.getChunnelHomePage().get(1).getShopId());
                    mContext.startActivity(intent);
                });

                home_main_head_365_3.setOnClickListener(v -> {
                    Intent intent = new Intent(mContext, ChannelOutActivity.class);
                    intent.putExtra("name", item.getChunnelHomePage().get(2).getName());
                    intent.putExtra("id", item.getChunnelHomePage().get(2).getShowGoodsCategoryId());
                    intent.putExtra("shopId", item.getChunnelHomePage().get(2).getShopId());
                    mContext.startActivity(intent);
                });
            }
        }
    }

    /**
     * 设置好店推荐数据
     *
     * @param helper
     * @param item
     */
    private void setShopRecommendsData(BaseViewHolder helper, HomeFindBean.EmbeddedBean.ContentBean item) {
        if (item.getChunnelHomePage().size() > 2) {
            setItemShow(helper, true);
        } else {
            setItemShow(helper, false);
        }
        RelativeLayout ll1 = helper.getView(R.id.rl_recommend);
        RelativeLayout ll2 = helper.getView(R.id.rl_recommend_2);
        RelativeLayout ll3 = helper.getView(R.id.rl_recommend_3);
        if (item.getChunnelHomePage().size() > 0) {
            if (item.getChunnelHomePage().size() == 1) {
                ll1.setVisibility(View.VISIBLE);
                ll2.setVisibility(View.INVISIBLE);
                ll3.setVisibility(View.INVISIBLE);

                XCRoundRectImageView ivShopOne = helper.getView(R.id.iv_shop_one);
                BaseImageLoaderUtils.getInstance().loadRamdomImage(context, ivShopOne, item.getChunnelHomePage().get(0).getImage());
                TextView tvRecommendLanguage = helper.getView(R.id.tv_recommend_language);
                tvRecommendLanguage.setText(item.getChunnelHomePage().get(0).getSlogan());
                TextView tvRecommendShopName = helper.getView(R.id.tv_recommend_shop_name);
                tvRecommendShopName.setText(item.getChunnelHomePage().get(0).getShopName());

                ll1.setOnClickListener(v -> {
                    if (!TextUtils.isEmpty(item.getChunnelHomePage().get(0).getShopId())) {
                        Intent intent = new Intent(mContext, NearShopGoodsActivity.class);
                        intent.putExtra("shopId", item.getChunnelHomePage().get(0).getShopId());
                        mContext.startActivity(intent);
                    }
                });
            }
            if (item.getChunnelHomePage().size() == 2) {
                ll1.setVisibility(View.VISIBLE);
                ll2.setVisibility(View.VISIBLE);
                ll3.setVisibility(View.INVISIBLE);

                XCRoundRectImageView ivShopOne = helper.getView(R.id.iv_shop_one);
                BaseImageLoaderUtils.getInstance().loadRamdomImage(context, ivShopOne, item.getChunnelHomePage().get(0).getImage());
                TextView tvRecommendLanguage = helper.getView(R.id.tv_recommend_language);
                tvRecommendLanguage.setText(item.getChunnelHomePage().get(0).getSlogan());
                TextView tvRecommendShopName = helper.getView(R.id.tv_recommend_shop_name);
                tvRecommendShopName.setText(item.getChunnelHomePage().get(0).getShopName());

                XCRoundRectImageView ivShopTwo = helper.getView(R.id.iv_shop_two);
                BaseImageLoaderUtils.getInstance().loadRamdomImage(context, ivShopTwo, item.getChunnelHomePage().get(1).getImage());
                TextView tvRecommendLanguage1 = helper.getView(R.id.tv_recommend_language_1);
                tvRecommendLanguage1.setText(item.getChunnelHomePage().get(1).getSlogan());
                TextView tvRecommendShopName1 = helper.getView(R.id.tv_recommend_shop_name_1);
                tvRecommendShopName1.setText(item.getChunnelHomePage().get(1).getShopName());

                ll1.setOnClickListener(v -> {
                    if (!TextUtils.isEmpty(item.getChunnelHomePage().get(0).getShopId())) {
                        Intent intent = new Intent(mContext, NearShopGoodsActivity.class);
                        intent.putExtra("shopId", item.getChunnelHomePage().get(0).getShopId());
                        mContext.startActivity(intent);
                    }
                });
                ll2.setOnClickListener(v -> {
                    if (!TextUtils.isEmpty(item.getChunnelHomePage().get(1).getShopId())) {
                        Intent intent = new Intent(mContext, NearShopGoodsActivity.class);
                        intent.putExtra("shopId", item.getChunnelHomePage().get(1).getShopId());
                        mContext.startActivity(intent);
                    }
                });

            }
            if (item.getChunnelHomePage().size() >= 3) {
                ll1.setVisibility(View.VISIBLE);
                ll2.setVisibility(View.VISIBLE);
                ll3.setVisibility(View.VISIBLE);

                XCRoundRectImageView ivShopOne = helper.getView(R.id.iv_shop_one);
                BaseImageLoaderUtils.getInstance().loadRamdomImage(context, ivShopOne, item.getChunnelHomePage().get(0).getImage());
                TextView tvRecommendLanguage = helper.getView(R.id.tv_recommend_language);
                tvRecommendLanguage.setText(item.getChunnelHomePage().get(0).getSlogan());
                TextView tvRecommendShopName = helper.getView(R.id.tv_recommend_shop_name);
                tvRecommendShopName.setText(item.getChunnelHomePage().get(0).getShopName());

                XCRoundRectImageView ivShopTwo = helper.getView(R.id.iv_shop_two);
                BaseImageLoaderUtils.getInstance().loadRamdomImage(context, ivShopTwo, item.getChunnelHomePage().get(1).getImage());
                TextView tvRecommendLanguage1 = helper.getView(R.id.tv_recommend_language_1);
                tvRecommendLanguage1.setText(item.getChunnelHomePage().get(1).getSlogan());
                TextView tvRecommendShopName1 = helper.getView(R.id.tv_recommend_shop_name_1);
                tvRecommendShopName1.setText(item.getChunnelHomePage().get(1).getShopName());

                XCRoundRectImageView ivShopThree = helper.getView(R.id.iv_shop_three);
                BaseImageLoaderUtils.getInstance().loadRamdomImage(context, ivShopThree, item.getChunnelHomePage().get(2).getImage());
                TextView tvRecommendLanguage2 = helper.getView(R.id.tv_recommend_language_2);
                tvRecommendLanguage2.setText(item.getChunnelHomePage().get(2).getSlogan());
                TextView tvRecommendShopName2 = helper.getView(R.id.tv_recommend_shop_name_2);
                tvRecommendShopName2.setText(item.getChunnelHomePage().get(2).getShopName());

                setShopParams(ivShopOne, ivShopTwo, ivShopThree);

                ll1.setOnClickListener(v -> {
                    if (!TextUtils.isEmpty(item.getChunnelHomePage().get(0).getShopId())) {
                        Intent intent = new Intent(mContext, NearShopGoodsActivity.class);
                        intent.putExtra("shopId", item.getChunnelHomePage().get(0).getShopId());
                        mContext.startActivity(intent);
                    }
                });
                ll2.setOnClickListener(v -> {
                    if (!TextUtils.isEmpty(item.getChunnelHomePage().get(1).getShopId())) {
                        Intent intent = new Intent(mContext, NearShopGoodsActivity.class);
                        intent.putExtra("shopId", item.getChunnelHomePage().get(1).getShopId());
                        mContext.startActivity(intent);
                    }
                });

                ll3.setOnClickListener(v -> {
                    if (!TextUtils.isEmpty(item.getChunnelHomePage().get(2).getShopId())) {
                        Intent intent = new Intent(mContext, NearShopGoodsActivity.class);
                        intent.putExtra("shopId", item.getChunnelHomePage().get(2).getShopId());
                        mContext.startActivity(intent);
                    }
                });
            }
        }
    }

    private void setShopParams(XCRoundRectImageView ivShopOne, XCRoundRectImageView ivShopTwo, XCRoundRectImageView ivShopThree) {

        int img_width = (SHelper.getScrrenWidth((Activity) mContext) - RxImageTool.dp2px(36)) / 3;

        ViewGroup.LayoutParams one_shop_params = ivShopOne.getLayoutParams();
        one_shop_params.width = img_width;
        one_shop_params.height = img_width;
        ivShopOne.setLayoutParams(one_shop_params);

        ViewGroup.LayoutParams two_shop_params = ivShopTwo.getLayoutParams();
        two_shop_params.width = img_width;
        two_shop_params.height = img_width;
        ivShopTwo.setLayoutParams(two_shop_params);

        ViewGroup.LayoutParams three_shop_params = ivShopThree.getLayoutParams();
        three_shop_params.width = img_width;
        three_shop_params.height = img_width;
        ivShopThree.setLayoutParams(three_shop_params);

    }

    /**
     * 设置限时购数据
     *
     * @param helper
     * @param item
     */
    private void setLimitedBuysData(BaseViewHolder helper, HomeFindBean.EmbeddedBean.ContentBean item) {
        int screenWidth = SHelper.getScrrenWidth((Activity) mContext);
        int img_width = (screenWidth - RxImageTool.dp2px(36)) / 3;
        if (item.getChunnelHomePage().size() > 0) {
            setItemShow(helper, true);
        } else {
            setItemShow(helper, false);
        }
        RoundCornersTransformation transformation = new RoundCornersTransformation(mContext,
                RxImageTool.dp2px(5), RoundCornersTransformation.CornerType.ALL);

        View xsgOne = helper.getView(R.id.home_main_xsg_one);
        View xsgMulti = helper.getView(R.id.home_main_xsg_multi);
        TextView limitMore = helper.getView(R.id.home_main_head_time_more);
        limitMore.setOnClickListener(v -> RouterCommonUtils.startNoParameterActivity(RouterIntentConstant.TIME_LIMIT));


        if (item.getChunnelHomePage().size() > 0) {
            HomeFindBean.EmbeddedBean.ContentBean.ChunnelHomePageBean chunnelHomePageBean = item.getChunnelHomePage().get(0);

            if (Preconditions.isNullOrEmpty(chunnelHomePageBean.getGoodsLimitedBuys())) {
                return;
            }

            if (chunnelHomePageBean.getGoodsLimitedBuys().size() == 1) {
                xsgOne.setVisibility(View.VISIBLE);
                xsgMulti.setVisibility(View.GONE);

                ImageView ivCountOne = helper.getView(R.id.home_main_count_one_iv);
                BaseImageLoaderUtils.getInstance().loadRamdomImage(context, ivCountOne, chunnelHomePageBean.getGoodsLimitedBuys().get(0).getImages());
                TextView tvOneGoodsName = helper.getView(R.id.home_main_count_one_name);
                TextView tvOneGoodsDescribe = helper.getView(R.id.home_main_count_one_describe);
                tvOneGoodsDescribe.setText(chunnelHomePageBean.getGoodsLimitedBuys().get(0).getOutline());
                tvOneGoodsName.setText(chunnelHomePageBean.getGoodsLimitedBuys().get(0).getTitle());
                TextView tvOneGoodsPrice = helper.getView(R.id.home_main_count_one_price);
                TextView tvOneGoodsOriginalPrice = helper.getView(R.id.home_main_count_one_gray);
                TextView tvOneGoodsOriginalBuy = helper.getView(R.id.home_main_count_one_buy);
                if (!TextUtils.isEmpty(chunnelHomePageBean.getGoodsLimitedBuys().get(0).getDiscountPrice())) {
                    tvOneGoodsPrice.setText(CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(chunnelHomePageBean.getGoodsLimitedBuys().get(0).getDiscountPrice())));
                    RxTextTool.getBuilder("").append("¥" + CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(chunnelHomePageBean.getGoodsLimitedBuys().get(0).getPrice()))).setStrikethrough().into(tvOneGoodsOriginalPrice);
                } else {
                    tvOneGoodsPrice.setText(CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(chunnelHomePageBean.getGoodsLimitedBuys().get(0).getPrice())));
                    tvOneGoodsOriginalPrice.setText("");
                }
                xsgOne.setOnClickListener(v ->
                        ARouter.getInstance()
                                .build(RouterIntentConstant.MODULE_SHOPPING_GOODS_DETAIL)
                                .withString("url", chunnelHomePageBean.getGoodsLimitedBuys().get(0).get_links())
                                .navigation());
                if (!TextUtils.isEmpty(chunnelHomePageBean.getStartTime()) && !TextUtils.isEmpty(chunnelHomePageBean.getEndTime())) {
                    try {
                        if (RxTimeTool.compare1(chunnelHomePageBean.getStartTime())) {
                            tvOneGoodsOriginalBuy.setText("开抢提醒");
                            tvOneGoodsOriginalBuy.setOnClickListener(v ->
                                    timeLimitModel.sendTips(Long.parseLong(chunnelHomePageBean.getGoodsLimitedBuys().get(0).getGoodsId()), chunnelHomePageBean.getStartTime(), new OnLoadListener<BaseResponseInfo>() {
                                        @Override
                                        public void onSuccess(BaseResponseInfo baseResponseInfo) {
                                            WpToast.s(mContext, baseResponseInfo.getMessage());
                                        }

                                        @Override
                                        public void onFail(String msg, int code) {
                                            WpToast.s(mContext, msg);
                                        }
                                    }));
                        } else if (RxTimeTool.compare1(chunnelHomePageBean.getEndTime())) {
                            tvOneGoodsOriginalBuy.setText("马上抢");
                            tvOneGoodsOriginalBuy.setOnClickListener(v ->
                                    ARouter.getInstance()
                                            .build(RouterIntentConstant.MODULE_SHOPPING_GOODS_DETAIL)
                                            .withString("url", chunnelHomePageBean.getGoodsLimitedBuys().get(0).get_links())
                                            .navigation());
                        } else {
                            tvOneGoodsOriginalBuy.setText("已结束");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } else if (chunnelHomePageBean.getGoodsLimitedBuys().size() >= 2) {
                xsgOne.setVisibility(View.GONE);
                xsgMulti.setVisibility(View.VISIBLE);

                ImageView iv1CountMulti1 = helper.getView(R.id.home_main_count_multi_img1);

                ViewGroup.LayoutParams lp_img1 = iv1CountMulti1.getLayoutParams();
                lp_img1.width = img_width;
                lp_img1.height = img_width;
                iv1CountMulti1.setLayoutParams(lp_img1);

                GlideApp.with(mContext)
                        .load(chunnelHomePageBean.getGoodsLimitedBuys().get(0).getImages())
                        .transform(transformation)
                        .placeholder(R.mipmap.image_default)
                        .error(R.mipmap.image_default)
                        .into(iv1CountMulti1);

                TextView tvMultiGoodsName1 = helper.getView(R.id.home_main_count_one_name_tv);
                tvMultiGoodsName1.setText(chunnelHomePageBean.getGoodsLimitedBuys().get(0).getTitle());
                TextView tvMultiGoodsPrice1 = helper.getView(R.id.home_main_count_multi_price1);
                TextView tvMultiGoodsOriginalPrice1 = helper.getView(R.id.home_main_count_multi_gray1);
                if (!TextUtils.isEmpty(chunnelHomePageBean.getGoodsLimitedBuys().get(0).getDiscountPrice())) {
                    tvMultiGoodsPrice1.setText(CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(chunnelHomePageBean.getGoodsLimitedBuys().get(0).getDiscountPrice())));
                    RxTextTool.getBuilder("").append("¥" + CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(chunnelHomePageBean.getGoodsLimitedBuys().get(0).getPrice()))).setStrikethrough().into(tvMultiGoodsOriginalPrice1);
                } else {
                    tvMultiGoodsPrice1.setText(CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(chunnelHomePageBean.getGoodsLimitedBuys().get(0).getPrice())));
                    tvMultiGoodsOriginalPrice1.setText("");
                }
                LinearLayout multiLl1 = helper.getView(R.id.home_main_count_multi_ll1);
                multiLl1.setOnClickListener(v -> ARouter.getInstance()
                        .build(RouterIntentConstant.MODULE_SHOPPING_GOODS_DETAIL)
                        .withString("url", chunnelHomePageBean.getGoodsLimitedBuys().get(0).get_links())
                        .navigation());
                if (chunnelHomePageBean.getGoodsLimitedBuys().size() >= 2) {
                    ImageView iv1CountMulti2 = helper.getView(R.id.home_main_count_multi_img2);

                    ViewGroup.LayoutParams lp_img2 = iv1CountMulti2.getLayoutParams();
                    lp_img2.width = img_width;
                    lp_img2.height = img_width;
                    iv1CountMulti2.setLayoutParams(lp_img2);

                    GlideApp.with(mContext)
                            .load(chunnelHomePageBean.getGoodsLimitedBuys().get(1).getImages())
                            .transform(transformation)
                            .placeholder(R.mipmap.image_default)
                            .error(R.mipmap.image_default)
                            .into(iv1CountMulti2);

                    TextView tvMultiGoodsName2 = helper.getView(R.id.home_main_count_two_name);
                    tvMultiGoodsName2.setText(chunnelHomePageBean.getGoodsLimitedBuys().get(1).getTitle());
                    TextView tvMultiGoodsPrice2 = helper.getView(R.id.home_main_count_multi_price2);
                    TextView tvMultiGoodsOriginalPrice2 = helper.getView(R.id.home_main_count_multi_gray2);
                    if (!TextUtils.isEmpty(chunnelHomePageBean.getGoodsLimitedBuys().get(1).getDiscountPrice())) {
                        tvMultiGoodsPrice2.setText(CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(chunnelHomePageBean.getGoodsLimitedBuys().get(1).getDiscountPrice())));
                        RxTextTool.getBuilder("").append("¥" + CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(chunnelHomePageBean.getGoodsLimitedBuys().get(1).getPrice()))).setStrikethrough().into(tvMultiGoodsOriginalPrice2);
                    } else {
                        tvMultiGoodsPrice2.setText(CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(chunnelHomePageBean.getGoodsLimitedBuys().get(1).getPrice())));
                        tvMultiGoodsOriginalPrice2.setText("");
                    }
                    LinearLayout multiLl2 = helper.getView(R.id.home_main_count_multi_ll2);
                    multiLl2.setOnClickListener(v -> ARouter.getInstance()
                            .build(RouterIntentConstant.MODULE_SHOPPING_GOODS_DETAIL)
                            .withString("url", chunnelHomePageBean.getGoodsLimitedBuys().get(1).get_links())
                            .navigation());
                }
                if (chunnelHomePageBean.getGoodsLimitedBuys().size() > 2) {
                    ImageView iv1CountMulti3 = helper.getView(R.id.home_main_count_multi_img3);

                    ViewGroup.LayoutParams lp_img3 = iv1CountMulti3.getLayoutParams();
                    lp_img3.width = img_width;
                    lp_img3.height = img_width;
                    iv1CountMulti3.setLayoutParams(lp_img3);

                    GlideApp.with(mContext)
                            .load(chunnelHomePageBean.getGoodsLimitedBuys().get(2).getImages())
                            .transform(transformation)
                            .placeholder(R.mipmap.image_default)
                            .error(R.mipmap.image_default)
                            .into(iv1CountMulti3);

                    TextView tvMultiGoodsName3 = helper.getView(R.id.home_main_count_three_name);
                    tvMultiGoodsName3.setText(chunnelHomePageBean.getGoodsLimitedBuys().get(2).getTitle());
                    TextView tvMultiGoodsPrice3 = helper.getView(R.id.home_main_count_multi_price3);
                    TextView tvMultiGoodsOriginalPrice3 = helper.getView(R.id.home_main_count_multi_gray3);
                    if (!TextUtils.isEmpty(chunnelHomePageBean.getGoodsLimitedBuys().get(2).getDiscountPrice())) {
                        tvMultiGoodsPrice3.setText(CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(chunnelHomePageBean.getGoodsLimitedBuys().get(2).getDiscountPrice())));
                        RxTextTool.getBuilder("").append("¥" + CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(chunnelHomePageBean.getGoodsLimitedBuys().get(2).getPrice()))).setStrikethrough().into(tvMultiGoodsOriginalPrice3);
                    } else {
                        tvMultiGoodsPrice3.setText(CharacterUtils.roundByGoodPrice(ConverterUtils.toDouble(chunnelHomePageBean.getGoodsLimitedBuys().get(2).getPrice())));
                        tvMultiGoodsOriginalPrice3.setText("");
                    }
                    LinearLayout multiLl3 = helper.getView(R.id.home_main_count_multi_ll3);
                    multiLl3.setOnClickListener(v -> ARouter.getInstance()
                            .build(RouterIntentConstant.MODULE_SHOPPING_GOODS_DETAIL)
                            .withString("url", chunnelHomePageBean.getGoodsLimitedBuys().get(2).get_links())
                            .navigation());
                }
            }
        }
    }


    @Override
    public void onViewAttachedToWindow(BaseViewHolder helper) {
        super.onViewAttachedToWindow(helper);
        if (helper.getItemViewType() == HomeFindBean.EmbeddedBean.ContentBean.CHUNNEL_LIMITED_BUYS_VIEW) {
            HomeFindBean.EmbeddedBean.ContentBean.ChunnelHomePageBean chunnelHomePageBean = getData().get(helper.getAdapterPosition()).getChunnelHomePage().get(0);
            if (!TextUtils.isEmpty(chunnelHomePageBean.getServerTime())) {
                long cur_time = System.currentTimeMillis();
                long difftime = RxTimeTool.string2Milliseconds(chunnelHomePageBean.getStartTime()) - cur_time;
                CountdownView timeCountView = helper.getView(R.id.home_main_head_time_count);
                if (difftime > 0) {
                    long countTime = RxTimeTool.string2Milliseconds(chunnelHomePageBean.getStartTime()) - cur_time;
                    initCountDown(countTime, timeCountView);
                } else {
                    long countTime = RxTimeTool.string2Milliseconds(chunnelHomePageBean.getEndTime()) - cur_time;
                    initCountDown(countTime, timeCountView);
                }
            }
        }
    }


    @Override
    public void onViewDetachedFromWindow(@NonNull BaseViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        if (holder.getItemViewType() == HomeFindBean.EmbeddedBean.ContentBean.CHUNNEL_LIMITED_BUYS_VIEW) {
            CountdownView timeCountView = holder.getView(R.id.home_main_head_time_count);
            timeCountView.stop();
        }
    }

    /**
     * 设置专题活动数据
     *
     * @param helper
     * @param item
     */
    private void setActivitiesData(BaseViewHolder helper, HomeFindBean.EmbeddedBean.ContentBean item) {
        if (item.getChunnelHomePage().size() > 0) {
            setItemShow(helper, true);
        } else {
            setItemShow(helper, false);
        }
        List<HomeFindBean.EmbeddedBean.ContentBean.ChunnelHomePageBean> typeYiData = new ArrayList<>();
        List<HomeFindBean.EmbeddedBean.ContentBean.ChunnelHomePageBean> typePinData = new ArrayList<>();
        if (item.getChunnelHomePage().size() > 0) {
            for (int i = 0; i < item.getChunnelHomePage().size(); i++) {
                HomeFindBean.EmbeddedBean.ContentBean.ChunnelHomePageBean bean = item.getChunnelHomePage().get(i);
                if ("1".equals(bean.getType())) {
                    typeYiData.add(bean);
                } else if ("2".equals(bean.getType())) {
                    typePinData.add(bean);
                }
            }
            if (typeYiData.size() > 0) {
                RecyclerView rvTypeYi = helper.getView(R.id.rv_type_zt_yi);
                LinearLayoutManager one_layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
                HomeMainActOneAdapter actOneAdapter = new HomeMainActOneAdapter(R.layout.module_shoppingguide_item_home_main_one, typeYiData);
                actOneAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);//加载动画
                rvTypeYi.setLayoutManager(one_layoutManager);
                rvTypeYi.setAdapter(actOneAdapter);
            }
            if (typePinData.size() > 0) {
                RecyclerView rvTypePin = helper.getView(R.id.rv_type_zt_pin);
                LinearLayoutManager pin_layoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
                HomeMainActPinAdapter pinAdapter = new HomeMainActPinAdapter(R.layout.module_shoppingguide_item_home_type_zt_pin, typePinData);
                rvTypePin.setLayoutManager(pin_layoutManager);
                rvTypePin.setAdapter(pinAdapter);
            }
        }
    }

    /**
     * 设置人气推荐数据
     *
     * @param helper
     * @param item
     */
    private void setRecommendsData(BaseViewHolder helper, HomeFindBean.EmbeddedBean.ContentBean item) {
        if (item.getChunnelHomePage().size() > 0) {
            setItemShow(helper, true);
        } else {
            setItemShow(helper, false);
        }
        RecyclerView homeTypeRqtj = helper.getView(R.id.rv_home_type_rqtj);
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 2);
        homeTypeRqtj.setLayoutManager(layoutManager);
        adapter = new HomeMainRecommendAdapter(R.layout.module_shoppingguide_item_home_type_rqtj_item, item.getChunnelHomePage());
        homeTypeRqtj.setAdapter(adapter);
    }

    /**
     * 设置新人专区数据
     *
     * @param helper
     * @param item
     */
    private void setChunnelNewbornData(BaseViewHolder helper, HomeFindBean.EmbeddedBean.ContentBean item) {
        if (item.getChunnelHomePage().size() > 0) {
            setItemShow(helper, true);
        } else {
            setItemShow(helper, false);
        }
        TextView tv_new_person_more = helper.getView(R.id.tv_new_person_more);
        tv_new_person_more.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, NewbornActivity.class);
            mContext.startActivity(intent);
        });
        RecyclerView rvNewborn = helper.getView(R.id.rv_new_person);
        NewbornHomeAdapter newbornHomeAdapter = new NewbornHomeAdapter(R.layout.module_shoppingguide_item_home_type_xrzq_item, item.getChunnelHomePage());
        rvNewborn.setLayoutManager(new LinearLayoutManager(mContext));
        rvNewborn.setAdapter(newbornHomeAdapter);
    }

    /**
     * 添加人气推荐数据
     *
     * @param data
     */
    public void addRecommendsData(List<HomeFindBean.EmbeddedBean.ContentBean.ChunnelHomePageBean> data) {
        adapter.addData(data);
        notifyDataSetChanged();
    }

    /**
     * 重新加载所有数据
     *
     * @param data
     */
    public void refreshAllData(List<HomeFindBean.EmbeddedBean.ContentBean> data) {
        this.mData.clear();
        addData(data);
    }


    private void setItemShow(BaseViewHolder helper, boolean isShow) {
        RecyclerView.LayoutParams param = (RecyclerView.LayoutParams) helper.itemView.getLayoutParams();
        if (isShow) {
            param.height = LinearLayout.LayoutParams.WRAP_CONTENT;
            param.width = LinearLayout.LayoutParams.MATCH_PARENT;
            helper.itemView.setVisibility(View.VISIBLE);
        } else {
            param.height = 0;
            param.width = 0;
            helper.itemView.setVisibility(View.GONE);
        }
        helper.itemView.setLayoutParams(param);
    }

    private void initCountDown(long leftTime, CountdownView limitTimer) {
        if (leftTime > 0) {
            limitTimer.start(leftTime);
        } else {
            limitTimer.stop();
            limitTimer.allShowZero();
        }
    }

}